<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */

namespace HiCommon\Repository;

use HiCommon\Model\ShopModify;

class ShopModifyRepository extends BaseRepository
{

    public function base_model(){
        return new \HiCommon\Model\ShopModify();
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return ShopModify::commonget($arr_option);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($arr_option = [] , array $arr_search = [] )
    {
        return ShopModify::where(
            function ($query)
            use($arr_search) {
                if(!empty($arr_search['name'])){
                    $query->where('name','like','%'.$arr_search['name'].'%');
                }
                if(!empty($arr_search['cate'])){
                    $query->where('cate',$arr_search['cate']);
                }
                if(!empty($arr_search['status'])){
                    $query->where('status',$arr_search['status']);
                }
            })->commonpage($arr_option);
    }


    /**
     *  统计用户的订单信息
     *  hinq 2019 10 25
     *
     **/
    public function get_user_to_order_count( array $arr_where ){

        $raw = \DB::raw("count('*') as total, sum('pay_price') as pay_price , user_id ");
        return ShopModify::select($raw)->where( $arr_where )->with('user')->groupBy('user_id')->get();
    }


    /**
     * 按用户统计数据（商户端用户列表专属）
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_user_list( array  $arr_where , string $act = 'asc' )
    {
        $raw = \DB::raw("count('*') as total, SUM(price) as pay_price , user_id ");
        return ShopModify::select($raw)->where( $arr_where )->orderBy('pay_price',$act)->with('user')->groupBy('user_id')->paginate(10);
    }



    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [],$arr_search = [])
    {
        return ShopModify::where($arr_where)
            ->where(
                function($query)
                use($arr_search) {
                    $n_is_call_helpdriving = intval($arr_search['is_call_helpdriving']);
                    if ( $n_is_call_helpdriving > 0) {
                        $query->whereHas('check_vehicle',
                            function ($query)
                            use($n_is_call_helpdriving){
                                $query->where('is_call_helpdriving', $n_is_call_helpdriving);
                            });

                    }
                }
            )
            ->where(
                function ($query)
                use($arr_search) {
                    $s_date = str_trim($arr_search['appointment_date']);
                    if(!empty($s_date)){
                        $query->where('appointment_date', $s_date);
                    }
                })
            ->commonpage($arr_option);

    }

    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return ShopModify::where($arr_where)->count();
    }

    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return ShopModify::where($arr_where)->sum($s_field);
    }

    /**
     * 根据条件批量删除信息
     *
     * @param array $arr_where
     * @param bool  $method
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_by_where($arr_where = [],$method = false)
    {
        return ShopModify::where($arr_where)
            ->commondelete($method);
    }

    /**
     * 通过where获取一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_where($arr_where,$arr_option = [])
    {
        return ShopModify::where($arr_where)->commonfirst($arr_option);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return ShopModify::create($arr_input);
    }

    /**
     * 添加多条数据
     *
     * @param array $arr_data
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_many($arr_data)
    {
        return ShopModify::insert($arr_data);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return ShopModify::commonfind($n_id, $arr_option);
    }

    /**
     * 更新或新建一条数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_or_create($arr_where,$arr_input){
        $res = ShopModify::where($arr_where)->first();
        if(is_null($res)){
            ShopModify::create(array_merge($arr_where,$arr_input));
        }
        else{
            ShopModify::where($arr_where)->update($arr_input);
        }
        return ShopModify::where($arr_where)->first();
    }



    /**
     * 根据where更新数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_by_wherein($arr_ids = [],$s_field = 'id'){
        return ShopModify::whereIn($s_field,$arr_ids)->update($arr_input);
    }


    /**
     * 根据where更新数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_by_where($arr_where,$arr_input){
        return ShopModify::where($arr_where)->update($arr_input);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return ShopModify::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return ShopModify::where('id', $n_id)
            ->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int    $n_id
     * @param int    $n_state
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state,$s_field = 'state')
    {
        return ShopModify::where('id',$n_id)->update([$s_field=>$n_state]);

    }

    /**
     * 修改一条数据状态
     *
     * @param string    $s_order_sn
     * @param int    $n_state
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state_by_ordersn(string $s_order_sn,int $n_state,$s_field = 'state')
    {
        return ShopModify::where('order_sn',$s_order_sn)->update([$s_field=>$n_state]);

    }

    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return ShopModify::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return ShopModify::where('id',$n_id)->decrement($s_field,$n_step);
    }







    /**
     * 分页获取数据-用户年检预约单列表
     *
     * @param array $arr_search
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_get_list_by_search($arr_search,$arr_option)
    {
        $arr_remain_key = ['pay_state','refund_state','appointment_state','check_state','appraise_state'];
        $arr_remain = array_remain($arr_search,$arr_remain_key);
        return ShopModify::where($arr_remain)->apipage($arr_option);
    }


    /**
     * 查询一条数据详情
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_get_detail(int $n_id, $arr_option = [])
    {
        $arr_option = [
            'with'=>['shop','user','shop_checkpoint_apprise','wechat_refund','check_vehicle.wechat_refund','check_vehicle.product','check_vehicle.helpdriving']
        ];
        return ShopModify::commonfind($n_id, $arr_option);
    }

    /**
     * 通过条件查询所有数据
     */
    public function get_all_by_where($map, $option = [])
    {
        return ShopModify::where($map)->get($option)->toArray();
    }

    /**
     * 通过条件查询所有可自动取消预约的订单
     */
    public function all_auto_cancel_order($arr_select = ['*'],$n_minutes = 30)
    {
        $arr_where = ['appointment_state'=>1, 'pay_state'=>1];
        $s_time = time_to_ymdhis(time() - 60 * $n_minutes);
        return ShopModify::where($arr_where)->where('created_at','<',$s_time)->select($arr_select)->get();
    }

    /*查询所有7天未评价订单*/
    public function all_auto_appointment_order_waiting_for_apprise($arr_select = ['*'],$n_day = 7){
        $arr_where = [
            'appointment_state'=>2,
            'pay_state'=>2,
            'check_state'=>2,
            'appraise_state'=>1,
        ];

        $s_time = time_to_ymdhis(time() - 60 * 60 * 24 * $n_day);
        return ShopModify::where($arr_where)->whereIn('refund_state',[0,2])->where('updated_at','<',$s_time)->select($arr_select)->get();
    }

    /*评价所有7天未评价订单*/
    public function apprise_all_waiting_appointment_order($n_day = 7){
        $arr_where = [
            'appointment_state'=>2,
            'pay_state'=>2,
            'check_state'=>2,
            'appraise_state'=>1,
        ];

        $s_time = time_to_ymdhis(time() - 60 * 60 * 24 * $n_day);
        $arr_data = ['appraise_state'=>2];
        return ShopModify::where($arr_where)->whereIn('refund_state',[0,2])->where('updated_at','<',$s_time)->update($arr_data);
    }

}